package com.sunny.websocket;

import java.io.IOException;
import java.net.URI;

import javax.websocket.ClientEndpoint;
import javax.websocket.DeploymentException;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;

import org.glassfish.tyrus.client.ClientManager;

@ClientEndpoint
public class WsClient {
	
	@OnOpen
	public void onOpen(Session session) {
		System.out.println("ws 连接...");
	}
	
	@OnClose
	public void onClose() {
		System.out.println("ws 关闭.");
	}
	
	@OnError
	public void onError(Throwable throwable) {
		System.out.println("ws ERROR.");
	}
	
	@OnMessage
	public void onMessage(String message) {
		System.out.println("收到消息：" + message);
	}
	
	public static void main(String[] args) {
		try {
			ClientManager client = ClientManager.createClient();
			Session session = client.connectToServer(WsClient.class, URI.create("ws://localhost:8080/websockt/3"));
			session.getBasicRemote().sendText("这是群消息");
			System.in.read();
			session.close();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (DeploymentException e) {
			e.printStackTrace();
		}
	}
}
